package 日程提醒工具.view;

import 日程提醒工具.controller.ScheduleReminder;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Scanner;

public class ConsoleView {
    private final ScheduleReminder scheduleReminder = new ScheduleReminder(this);
    private final Scanner scanner;
    private int count;  // 用来计算输入的空行数，空行数等于5将进行提示(隐藏功能)

    public ConsoleView(Scanner scanner) {
        this.scanner = scanner;
    }

    public void printMenu() {
        System.out.print("""
                ===== 菜单栏 =====
                [1]添加任务
                [2]查看未完成任务
                [3]查看已完成任务
                [4]管理任务
                [5]保存并退出
                """);
    }

    public void showInputReminder() {
        // 此if语句必须放在打印提示符语句之前，这样才会在第三次打印提示符之后调用help方法，才能符合逻辑。
        if (count == 5) {
            help();
            count = 0;
        }

        System.out.print('>');
    }

    private void help() {
        System.out.println();
        System.out.println("[提示] 输入\"help\"可以显示菜单");
    }

    private int getOperationNumber() {
        final int MAX_OPERATION_NUMBER = 5;
        final String ERROR = "输入失败！请输入一个1到" + MAX_OPERATION_NUMBER + "之间的整数！";
        int operationNumber;

        showInputReminder();
        while (true) {
            String line = scanner.nextLine().trim();

            try {
                int input = Integer.parseInt(line);
                if (input > 0 && input <= MAX_OPERATION_NUMBER) {
                    operationNumber = input;
                    break;
                } else {    // 防止输入不在操作编码内的数字
                    System.out.println(ERROR);
                    showInputReminder();
                }
            } catch (NumberFormatException e) {
                if (!line.isEmpty()) {   // 防止输入多空行加字符串
                    if ("help".equals(line)) {
                        printMenu();
                        count = 0;
                    } else {
                        System.out.println(ERROR);
                    }
                } else {
                    count++;    // 只有在打印空行的时候count才会增加
                }
                showInputReminder();
            }
        }
        return operationNumber;
    }

    public String inputTitle() {
        System.out.print("请输入标题（一行）：");
        return scanner.nextLine();
    }

    public String inputDescription() {
        System.out.print("请输入描述（一行）：");
        return scanner.nextLine();
    }

    private void printFormatError() {
        System.out.println("输入格式错误！请重新输入！");
    }

    public LocalDateTime inputDeadline() {
        final String REMINDER = "请输入截止时间（yyyy-MM-dd HH:mm）：";

        System.out.print(REMINDER);
        while (true) {
            try {
                String[] inputs = scanner.nextLine().split("\\D+", 5);
                int year = Integer.parseInt(inputs[0]);
                int month = Integer.parseInt(inputs[1]);
                int day = Integer.parseInt(inputs[2]);
                int hour = Integer.parseInt(inputs[3]);
                int minute = Integer.parseInt(inputs[4]);

                return LocalDateTime.of(year, month, day, hour, minute);
            } catch (Exception e) {
                printFormatError();
                System.out.print(REMINDER);
            }
        }
    }

    public int inputId() {
        final String REMINDER = "请输入要设置的日期的id：";

        System.out.print(REMINDER);
        while (true) {
            if (scanner.hasNextInt()) {
                int input = scanner.nextInt();
                scanner.nextLine();

                return input;
            } else {
                String line = scanner.nextLine();
                if (!line.trim().isEmpty()) {   // 防止输入多空行加字符串
                    printFormatError();
                    System.out.print(REMINDER);
                }
            }
        }
    }

    public Boolean yesOrNo() {
        while (true) {
            String input = scanner.nextLine();
            if ("yes".equalsIgnoreCase(input) || "y".equalsIgnoreCase(input)) {
                return true;
            } else if ("no".equalsIgnoreCase(input) || "n".equalsIgnoreCase(input)) {
                return false;
            }
            printFormatError();
        }
    }

    public void run() throws IOException {
        scheduleReminder.initialize();
        while (true) {
            switch (getOperationNumber()) {
                case 1 -> scheduleReminder.addSchedule();
                case 2 -> scheduleReminder.getUndoneSchedules();
                case 3 -> scheduleReminder.getDoneSchedules();
                case 4 -> scheduleReminder.manageSchedule();
                case 5 -> {
                    scheduleReminder.save();
                    System.out.println("退出！");
                    return;
                }
            }
        }
    }
}
